import {playNumUp,addNumber,getHistoryById} from '../api/index'
import {mapGetters} from 'vuex'

export const mixin = {
  computed:{
    ...mapGetters([
      'loginIn',
      'userId'
    ])
  },
  methods:{
    notify(title,type){
      this.$notify({
        title:title,
        type:type
      })
    },
    getUrl(url){
      return url?this.$store.state.HOST+url : this.$store.state.HOST+'/avatarImages/user.jpg'
    },
    //返回多少层
    goback (index) {
      this.$router.go(index)
    },
    //按名字或类型搜索
    goSearch(type){
      if (type == 'name' && this.search_text != '') {
        const {href} = this.$router.resolve({
                      path: `/search?text=${this.search_text}`});
        window.open(href, '_blank');
      }else if(type > 0 && type < 20){
        const {href} = this.$router.resolve({
                      path: `/search?style=${type}`});
        window.open(href, '_blank');
      }
    },
    //格式化时间
    formatTime(value){
      let theTime = parseInt(value)
      let result = ''
      let hour = parseInt(theTime/3600)
      let minute = parseInt(theTime/60%60)
      let second = parseInt(theTime%60)
      if(hour > 0){
        if(hour < 10){
          result = '0' + hour +':'
        }else{
          result = hour + ':'
        }
      }
      if (minute > 0) {
        if(minute < 10){
          result += 0 + minute + ':'
        }else{
          result += minute + ':'
        }
      }else{
        result += '00:'
      }
      if (second > 0) {
        if (second < 10) {
          result += '0' +second
        }else{
          result += second
        }
      }else{
        result += '00'
      }
      return result
    },
    //格式化日期
    attachDate (val) {
      let Date = String(val).match(/[0-9-]+(?=\s)/)
      return Array.isArray(Date) ? Date[0] : Date
    },
    gotoPlayVideo(e){
      let i = 0
      //获取点击标签，找到path上标签className为play的标签，获取标签上的id值(视频id)
      while (e.path[i].classList && !e.path[i].classList.contains('play') && i < 4) {
        i++
      }
      if(e.path[i].attributes.videoid){
        var id = e.path[i].attributes.videoid.value;//视频id
        if(id != null){
          const {href} = this.$router.resolve({
                    path: `/playVideo?id=${id}`});
              window.open(href, '_blank');
          if(e.path[i].classList.contains('play') && e.path[i].attributes.videoid){
            if(this.loginIn){
              
              playNumUp(id)  //播放量增加
    
              let formData = new FormData()
              formData.append('id',this.userId)
              formData.append('value',id)
              formData.append('type',1) //0收藏，1历史 2点赞
              addNumber(formData) //将该视频录入用户历史记录
    
              setTimeout(() => { //0.2s后刷新页面历史记录
                getHistoryById(this.userId)
                  .then(res =>{
                    var history = res.videoIds.split('-')
                    this.$store.commit('setHistory',history)
                  })
              }, 200);
            }
          }
        }
      }
    },
    changeNumber(val){
      if(val > 99999999){
        if(val/100000000 > 999){
          return (val/100000000).toFixed(0)+'亿'
        }else{
          return (val/100000000).toFixed(1)+'亿'
        }
      }else if(val > 9999){
        if(val/10000 > 999){
          return (val/10000).toFixed(0)+'万'
        }else{
          return (val/10000).toFixed(1)+'万'
        }
      }else{
        return val
      }
    }
  }
}